package com.sunthy.mall.service.admin.user;

import com.sunthy.mall.dao.admin.UserDao;
import com.sunthy.mall.model.admin.bo.admin.AdminLoginBo;
import com.sunthy.mall.model.admin.bo.client.user.UpdatePwdBo;
import com.sunthy.mall.model.admin.bo.client.user.UpdateUserDataBo;
import com.sunthy.mall.model.admin.pojo.admin.User;
import com.sunthy.mall.model.admin.vo.admin.AllUserVO;
import com.sunthy.mall.model.admin.vo.client.user.UserDataVo;
import com.sunthy.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 实现 ForeUserService
 * @Author: XM
 */
public class UserServiceImpl implements UserService {

    /**
     * 查看所有user信息
     *
     * @return List<AllUserVO>
     */
    public List<AllUserVO> allUser() {
        SqlSession sqlSession = MybatisUtils.openSession();
        UserDao adminDao = sqlSession.getMapper(UserDao.class);
        // 查询数据库
        List<User> userList = adminDao.allUser();
        // 关闭会话
        sqlSession.commit();
        sqlSession.close();
        // 返回结果
        return getAllAdminVOSList(userList);
    }

    /**
     * 根据id删除user
     *
     * @param id
     * @return int 0  10000
     */
    public int delete(int id) {
        SqlSession sqlSession = MybatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        // 去数据库中删除数据 返回修改的行数 成功返回1
        int delete = userDao.delete(id);
        sqlSession.commit();
        sqlSession.close();

        if (delete != 1) { // 失败
            return 10000;
        }
        // 成功
        return 0;
    }

    /**
     * 根据nickname模糊查询user
     *
     * @param nickname
     * @return
     */
    public List<AllUserVO> searchAllUser(String nickname) {
        SqlSession sqlSession = MybatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        // 去数据库查询user
        List<User> userList = userDao.searchAllUser(nickname);
        sqlSession.commit();
        sqlSession.close();

        return getAllAdminVOSList(userList);
    }

    /**
     * 登录user
     *
     * @param adminLoginBo
     * @return string
     */
    @Override
    public String userLogin(AdminLoginBo adminLoginBo) {
        // 获取会话
        SqlSession sqlSession = MybatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 查询数据库
        User selUser = userDao.selUser(adminLoginBo);
        // 关闭会话
        sqlSession.commit();
        sqlSession.close();
        // 判断
        if (selUser == null) {
            return null;
        }
        return selUser.getUsername();
    }

    /**
     * 添加user
     *
     * @param allUserVO
     * @return int
     */
    @Override
    public int signUpUser(AllUserVO allUserVO) {
        // 获取会话
        SqlSession sqlSession = MybatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 查询 user的Email不可重负
        List<User> userList = userDao.allUserByEmail(allUserVO.getEmail());
        if (!userList.isEmpty()) {
            sqlSession.commit();
            sqlSession.close();
            return 500;
        }
        // 添加user
        userDao.addUser(allUserVO);
        sqlSession.commit();
        sqlSession.close();
        return 0;
    }

    /**
     * 获取user的资料
     *
     * @param token
     * @return UserDataVo
     */
    @Override
    public UserDataVo getUserData(String token) {
        // 获取会话
        SqlSession sqlSession = MybatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 查询数据
        List<User> userList = userDao.allUserByEmail(token);
        // 关闭会话
        sqlSession.commit();
        sqlSession.close();
        if (userList.size() != 1) {
            return null;
        }
        // 返回结果
        User user = userList.get(0);
        return new UserDataVo(
                0,
                user.getId(),
                token,
                user.getNickname(),
                user.getRecipient(),
                user.getAddress(),
                user.getPhone()
        );


    }

    /**
     * 修改密码
     *
     * @param updatePwdBo
     * @return int
     */
    @Override
    public int updatePwd(UpdatePwdBo updatePwdBo) {
        // 获取会话
        SqlSession sqlSession = MybatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 修改密码
        String oldPwd = userDao.selUserById(updatePwdBo.getId());
        if (!updatePwdBo.getOldPwd().equals(oldPwd)) {
            sqlSession.commit();
            sqlSession.close();
            return 404;
        }
        int code = userDao.updateUserPwd(updatePwdBo);
        // 关闭会话
        sqlSession.commit();
        sqlSession.close();
        // 返回结果
        if (code == 1) {
            return 0;
        }
        return 500;
    }

    /**
     * 修改user
     *
     * @param updateUserDataBo
     * @return int
     */
    @Override
    public int updateUserData(UpdateUserDataBo updateUserDataBo) {
        // 获取会话
        SqlSession sqlSession = MybatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 更改user
        int code = userDao.updateUSer(updateUserDataBo);
        // 关闭会话
        sqlSession.commit();
        sqlSession.close();
        // 返回结果
        if (code == 1) {
            return 0;
        }
        return 500;
    }


    /**
     * 将返回的UserList 封装成 AllUserVoList
     *
     * @param userList
     * @return userVOS
     */
    private List<AllUserVO> getAllAdminVOSList(List<User> userList) {
        List<AllUserVO> userVOS = new ArrayList<AllUserVO>();
        for (User user : userList) {
            AllUserVO adminVO = new AllUserVO(user.getId(), user.getUsername(), user.getNickname(), user.getPassword(),
                    user.getRecipient(), user.getAddress(), user.getPhone());
            userVOS.add(adminVO);
        }
        return userVOS;
    }
}
